package com.mo.security.controller;

import com.mo.security.common.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 权限测试，可以自由组合
 * 如果是以 ROLE_ 开头，SpringSecurity 会把他当成是角色，注意是严格区分大小写
 */
@RestController
@RequestMapping("auth")
public class AuthTestController {

    @GetMapping("t1")
    @PreAuthorize("hasAuthority('sys:get')")
    public Result t1() {
        return Result.success("访问成功", "hasAuthority('sys:get')");
    }

    @GetMapping("t2")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result t2() {
        return Result.success("访问成功", "hasRole('ROLE_ADMIN')");
    }

    @GetMapping("t3")
    @PreAuthorize("hasAnyAuthority('sys:add','sys:delete','sys:edit','sys:get')")
    public Result t3() {
        return Result.success("访问成功", "hasAnyAuthority('sys:add','sys:delete','sys:edit','sys:get')");
    }

    @GetMapping("t4")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public Result t4() {
        return Result.success("访问成功", "hasAnyRole('ROLE_ADMIN','ROLE_USER')");
    }

    @GetMapping("t5")
    @PreAuthorize("hasAuthority('sys:delete') and hasRole('ROLE_ADMIN')")
    public Result t5() {
        return Result.success("访问成功", null);
    }

    @GetMapping("t6")
    @PreAuthorize("hasAuthority('sys:add') or hasRole('ROLE_ADMIN')")
    public Result t6() {
        return Result.success("访问成功", null);
    }
}
